package com.cleargroup.magic.cleara.c.command;

import com.cleargroup.magic.cleara.c.BatteryHelper;

import java.io.File;
import java.util.regex.Pattern;


/**
 * ////////////////////////////////////// AIO 5.3.5 ////////////////////////////////////////////////
 * "/cache"
 * ".cache"
 * ".tmp"
 * "thumbnails"
 * "iconcache"
 * ".log"
 *
 * ////////////////////////////////////// AIO 5.3.6 ////////////////////////////////////////////////
 * trash keyword:
 * /////////////////////////////////////////////////////////////////////////////////////////////////
 * "/cache"
 *   				计算机/Galaxy S5/Phone/Android/data/com.dailyyoga.inc/cache
 *   				/storage/emulated/0/183/LizhiFM/Caches/tmp
 *   				/storage/emulated/0/UCDownloads/cache/com.eg.android.AlipayGphone
 *   				/storage/emulated/0/UCDownloads/cache/com.eg.android.AlipayGphone/httpCache/f0272faca795e65e
 *   				/storage/emulated/0/youku/cacheData/16d8f8576ccc6b846a5de206492b5857
 *   				/storage/emulated/0/GoAdSdk/advert/cacheFile
 *   				计算机/Galaxy S5/Phone/Android/data/com.facebook.katana/cache
 *   				计算机/Galaxy S5/Phone/Android/data/com.alensw.PicFolder/cache/thumb_123904.db
 *   				计算机/Galaxy S5/Phone/VideoCache/com.tencent.mm
 *   				计算机/Galaxy S5/Phone/Android/data/com.starbucks.cn/cache/uil-images/journal
 *   				计算机/Galaxy S5/Phone/Android/data/com.sec.android.gallery3d/cache/attributecache.idx
 * "_cache"
 *   				/storage/emulated/0/kbatterydoctor/app_cache/aHR0cDovL2RsLmNtLmtzbW9iaWxlLmNvbS9zdGF0aWMvcmVzLzg2L2NmL2d1aWRlLnBuZw==.png
 *   				计算机/Galaxy S5/Phone/umeng_cache/656AC1B6964F754B4BB38F841ED6C2B7
 *   				/storage/emulated/0/KuwoMusic/.data/CROWDFUNDING_CACHE
 *   				计算机/Galaxy S5/Phone/Android/data/com.alensw.PicFolder/folder_cache
 * "cache_"
 *   				/storage/emulated/0/tencent/MobileQQ/diskcache/Cache_251dfb119b340e0d
 * ".cache"
 *   				/storage/emulated/0/zsfile/.cache/icon/imoblife.memorybooster.lite.png
 *   				/storage/emulated/0/youku/ikuacc/cache.dat
 * "cache."
 *   				/storage/emulated/0/com.jd.jdsdk/module/app_cart/3.8/js/myViewWithCache.js
 *   				/storage/emulated/0/tencent/tassistant/webview_cache/cache.db
 *   				/storage/emulated/0/tencent/MicroMsg/diskcache/cache.data.2
 *   				/storage/emulated/0/tencent/MicroMsg/WebviewCache/sfs/wvcache.block.00000
 * "/tmp"
 *   				/storage/emulated/0/system/tmp/local/tdata_wWg387
 *   				/storage/emulated/0/system/tmp/local/tdata_tLH487
 *   				/storage/emulated/0/system/tmp/local/tdata_HpS907
 *   				计算机/Galaxy S5/Phone/tuicool/images/tmp/-7209028
 * ".tmp"
 *   				/storage/emulated/0/com.facebook.orca/fb_temp/sticker1457701288623.tmp
 * ".temp"
 *   				/storage/emulated/0/360Download/com.longtugame.jymf.qihoo22.apk.temp!
 *   				/storage/emulated/0/360Download/com.longtugame.jymf.qihoo22.apk.temp
 *   				/storage/emulated/0/360Download/com.longtugame.jymf.qihoo22-1458724839060.apk.temp!
 *   				/storage/emulated/0/360Download/com.longtugame.jymf.qihoo22-1458724839060.apk.temp
 *   				/storage/emulated/0/tencent/MicroMsg/db555a55dddcb111df4cec11e711924b/image2/50/c6/50c650525234fe28868e0875a68fe960.temp.jpg
 *   				/storage/emulated/0/tencent/MicroMsg/db555a55dddcb111df4cec11e711924b/image2/ce/d4/ced4051e0d420aba63c820b5664dbf26.temp
 * "_temp"
 *   				/storage/emulated/0/tencent/zebra/weatherSentence/weatherSentence_temp.png
 *   				/storage/emulated/0/tencent/MicroMsg/db555a55dddcb111df4cec11e711924b/sfs/sns/temp/pre_temp_sns_piceaa7982254bdd381b29344f44a3a099f
 * "/temp"
 *   				/storage/emulated/0/temp/qiyi/cube_feedback.txt
 *   				/storage/emulated/0/temp/qiyi/psnetwork.ini
 *   				/storage/emulated/0/temp/qiyi/vodservercfg.blf
 *   				/storage/emulated/0/temp/qiyi/PowerPlayer.ini
 *    			    /storage/sdcard0/baidu/tempdata/ls.db-journal
 *   				计算机/Galaxy S5/Phone/Android/data/com.evernote/files/Temp
 * "/p2pcache"
 *   				/storage/emulated/0/KuwoMusic/p2pcache/2765977432.1308913133
 *   				/storage/emulated/0/KuwoMusic/p2pcache/2765977432.1308913133.map
 *   				/storage/emulated/0/KuwoMusic/p2pcache/1907358184.2573753057
 *   				/storage/emulated/0/KuwoMusic/p2pcache/1907358184.2573753057.map
 * "thumbnails"
 *   				/storage/emulated/0/DCIM/.thumbnails/.thumbdata3--1967290299
 *   				/storage/emulated/0/DCIM/.thumbnails/.thumbindex3--1967290299
 *   				/storage/emulated/0/DCIM/.thumbnails/1458541679815.jpg
 *   				/storage/emulated/0/DCIM/.thumbnails/1458646059712.jpg
 * "imgcache"
 *   				/storage/emulated/0/moji/ugcImgCache/D9A82C7C6A3C41A3C4448FBA3BE35B0F
 *   				/storage/emulated/0/Android/data/com.qiyi.video/cache/adcache/5dddfcd45165b9115c7be097977d782e.f4v
 * "imagecache"
 *   				/storage/emulated/0/tencent/pb/imagecache/contactphoto/2a6cd1ff4da2529b499cd40822dc956d.0
 * "thumbnailcache"
 *   				/storage/emulated/0/tencent/tassistant/ThumbnailCache/icon/ED77D0E6AE6D940AC5446E884C664BD9
 * "iconcache"
 * "thumb"
 *   				计算机/Galaxy S5/Phone/Android/data/com.evernote/files/announcements/a1/thumb1
 * “/log”
 *   				计算机/Galaxy S5/Phone/Android/data/com.bj8264.zaiwai.android/beijing8264#zaiwaiapp/log/20160301/000.html
 * ".log"
 * "log."
 * "log_"
 * "/logs"
 *   				计算机/Galaxy S5/Phone/yymobile/logs/logs.txt
 * /////////////////////////////////////////////////////////////////////////////////////////////////
 * protect keyword:
 * /////////////////////////////////////////////////////////////////////////////////////////////////
 * ".mp3"
 * ".nomedia"
 *  				/storage/emulated/0/kbatterydoctor/app_cache/.nomedia
 * "com.spotify"
 *                  /sd/spotify/.shared/
 * "com.google"
 * "offline"
 * /////////////////////////////////////////////////////////////////////////////////////////////////
 * 不能过滤出来的缓存：
 * /////////////////////////////////////////////////////////////////////////////////////////////////
 * /storage/emulated/0/sina/weibo/.weibo_pic_new
 * 点击微博图片时产生的大图
 * /storage/emulated/0/sina/weibo/.prenew
 * 微博的图片缩略图
 * /storage/emulated/0/sina/weibo/.portraitnew
 * 微博的用户图像
 * /storage/emulated/0/Android/data/.log/com.sina.weibo
 * 微博的日志文件
 * /storage/emulated/0/sina/weibo/theme_data
 * 微博的主题数据缓存
 * /storage/emulated/0/sina/weibo/msgcatch
 * 微博产生的消息缓存
 * /storage/emulated/0/sina/weibo/weiyoumenulist
 * 微博的微友列表缓存
 * /storage/emulated/0/sina/weibo/.snggamemsg
 * 微博产生的游戏临时缓存
 * /storage/emulated/0/Android/data/com.facebook.orca/files/stickers
 * Facebook的贴纸缓存
 */
public class TrashHelper {
    private static final String TAG = TrashHelper.class.getSimpleName();

    public static final int TRASH_NORMAL = -1;
    public static final int TRASH_TEMPERARY = 0;
    public static final int TRASH_THUMBNAIL = 1;
    public static final int TRASH_INVALID = 2;
    public static final int TRASH_LOGFILE = 3;
    public static final int TRASH_EMPTY = 4;
    public static final int TRASH_APK = 5;
    public static final int TRASH_LARGEFILE = 6;

    private static final Pattern TEMP_PATTERN = Pattern.compile("[^a-zA-Z0-9](cache|caches|tmp|temp[^a-zA-Z0-9])");
    private static final Pattern TEMP_PATTERN2 = Pattern.compile("(cache|caches|tmp|temp)[^a-zA-Z0-9]");
    private static final Pattern THUMB_PATTERN = Pattern.compile("[^a-zA-Z0-9](thumbnails|iconcache|imgcache|imagecache)");
    private static final Pattern LOG_PATTERN = Pattern.compile("[^a-zA-Z0-9](log|logs)[^a-zA-Z0-9]");
    public static final long LARGE_FILE_SIZE = 10 * 1024 * 1024L;

    public static int detect(File file) {
        String uri = file.getAbsolutePath();

        if (file.isDirectory()) {
            if (BatteryHelper.isEmptyDir(file)) {
                return TRASH_EMPTY;
            }

        } else {
            //temp keyword
            if (TEMP_PATTERN.matcher(uri).find() || TEMP_PATTERN2.matcher(uri).find()) {
                return TRASH_TEMPERARY;
            }

            //thumb keyword
            else if (THUMB_PATTERN.matcher(uri).find()) {
                return TRASH_THUMBNAIL;
            }

            //log keyword
            else if (LOG_PATTERN.matcher(uri).find()) {
                return TRASH_LOGFILE;
            }

            //invalid keyword
            else if (uri.contains("lost.dir")) {
                return TRASH_INVALID;
            }

            //apk keyword
            else if (uri.endsWith(".apk")) {
                return TRASH_APK;
            }

            //large file keyword
            else if (file.length() > LARGE_FILE_SIZE) {
                return TRASH_LARGEFILE;
            }

        }

        return TRASH_NORMAL;
    }
}
